<?php
namespace app\api\controller;
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods:POST,GET");

use app\common\controller\HomeBase;
use app\common\model\Nav         as NavModel;
use app\common\model\NavNews     as NavNewsModel;
use app\common\model\NavSolution as NavSolutionModel;
use app\common\model\NavAnli     as NavAnliModel;
use think\Db;

/**
 * 解决方案
 * Class Index
 * @package app\index\controller
 */
class Solutions extends HomeBase
{
    protected function _initialize(){
        parent::_initialize();
        $this->nav_model    = new NavModel();
        $this->nav_news     = new NavNewsModel();
        $this->nav_solution = new NavSolutionModel();
        $this->nav_anli     = new NavAnliModel();
    }

	/**
    *解决方案的显示
    *@return json
    **/
    public function show()
    {
        $nav_id  = $this->request->param('nav_id');
        $this->request->param('limit') ? $limit = $this->request->param('limit') : $limit = 6;
        $this->request->param('page')  ? $page = $this->request->param('page') : $page = 1;
        //判断传过来的是否还有子栏目
        $navlist = $this->nav_model->where('status',1)->select();
        //子栏目的导航id
        $arr = array2array($navlist,$nav_id);
        $map = [];
        if($arr){
           	$map['nav_id'] = ['in',$arr];
            $newslist3 = $this->nav_solution->where($map)->where('status',1)->select();
            $newslist1 = $this->nav_solution->where($map)->where('status',1)->order('create_time desc,top desc')->paginate($limit, false, ['page' => $page]);
            
        }else{
            $newslist3 = $this->nav_solution->where('status',1)->where('nav_id',$nav_id)->select();
            $newslist1 = $this->nav_solution->where('status',1)->where('nav_id',$nav_id)->order('create_time desc,top desc')->paginate($limit, false, ['page' => $page]);
        }
            if($newslist1){
                $newslist1 = $newslist1->toArray();
                $list = getlistphoto($newslist1['data']);
                $total = count($newslist3);
                $info  = [
                    'list'  => $list,
                    'total' => $total,
                    'page'  => $page,
                ];
                $this->apiSuccess($info);
            }else{
                $this->apiError('没有查到信息');
            }


    }

    /**
    *单个解决方案的显示
    *@return json
    **/
    public function showDetail()
    {
        $id      = $this->request->param('id');
        $detail  = $this->nav_solution->where('id',$id)->select();
        if($detail){
            //获得上一个下一个题目
            $top  = $this->getUpDownTitle($id,'nav_solution',1);
            $down = $this->getUpDownTitle($id,'nav_solution',0);
            $detail1 = getlistphoto($detail);
            //将内容格式化
            $detail1[0]['content'] = htmlspecialchars_decode($detail1[0]['content']);
            //阅读量+1
            $readnumber = $detail[0]['readnumber'] + 1;
            $list = $this->nav_solution->save(['readnumber'=>$readnumber],['id'=>$id]);
            if($list){
                $list = [
                    'list' =>$detail1[0],
                    'top'  =>$top,
                    'down' =>$down,
                ];
                $this->apiSuccess($list);
            }else{
                $this->apiError('更新数据出错,请重试');
            }
        }else{
            $this->apiError('没有查到信息');
        }
    }

    /**
    *解决方案的上一个下一个
    *@return json
    **/
    public function solutionUpDown()
    {
        $id  = $this->request->param('id');
        $top = $this->request->param('top');
        $info = $this->getUpDownId($id,"nav_solution",$top);
            if (is_int($info)) {
                //上一篇下一篇文章标题
                $top  = $this->getUpDownTitle($info,'nav_solution',1);
                $down = $this->getUpDownTitle($info,'nav_solution',0);

                $detail  = $this->nav_solution->where('id',$info)->select();
                if($detail){
                    $detail1 = getlistphoto($detail);
                    //将内容格式化
                    $detail1[0]['content'] = htmlspecialchars_decode($detail1[0]['content']);
                    //阅读量+1
                    $readnumber = $detail[0]['readnumber'] + 1;
                    $list = $this->nav_solution->save(['readnumber'=>$readnumber],['id'=>$info]);
                    if($list){
                        $listInfo = [
                            'list'=>$detail[0],
                            'top'=>$top,
                            'down'=>$down,
                        ];
                        $this->apiSuccess($listInfo);
                    }else{
                        $this->apiError('更新数据出错,请重试');
                    }
                }else{
                    $this->apiError('没有查到信息');
                }
            }else{
                $this->apiError('没有查到信息');
            }
    }

    /**
    *置顶的news显示
    *@return json
    **/
    public function topSolutionShow()
    {
        $nav_id  = $this->request->param('nav_id');
        $this->request->param('limit') ? $limit = $this->request->param('limit') : $limit = 6;
        $list = $this->showTop($nav_id,$limit,'nav_solution');
        if($list){
            $this->apiSuccess($list);
        }else{
            $this->apiError('没有查到信息');
        }
    }

    /**
    *news显示
    *@return json
    **/
    public function newsshow()
    {
        $nav_id  = $this->request->param('nav_id');
        $this->request->param('limit') ? $limit = $this->request->param('limit') : $limit = 6;
        $this->request->param('page')  ? $page = $this->request->param('page') : $page = 1;
        //判断传过来的是否还有子栏目
        $navlist = $this->nav_model->where('status',1)->select();
        //子栏目的导航id
        $arr = array2array($navlist,$nav_id);
        $map = [];
        if($arr){
            $map['nav_id'] = ['in',$arr];
            $newslist3 = $this->nav_news->where($map)->where('status',1)->select();
            $newslist  = $this->nav_news->where($map)->where('status',1)->order('create_time desc,top desc')->paginate($limit, false, ['page' => $page]);
            $newslist  = $newslist->toArray();
        }else{
            $newslist3 = $this->nav_news->where('nav_id',$nav_id)->where('status',1)->select();
            $newslist  = $this->nav_news->where('nav_id',$nav_id)->where('status',1)->order('create_time desc,top desc')->paginate($limit, false, ['page' => $page]);
            $newslist  = $newslist->toArray();
        } 
            if($newslist){
                $list = getlistphoto($newslist['data']);
                $total = count($newslist3);
                $info  = [
                    'list'  => $list,
                    'total' => $total,
                    'page'  => $page,
                ];
                $this->apiSuccess($info);
            }else{
                $this->apiError('没有查到信息');
            }
    }

    /**
    *单个新闻
    *@return json
    **/
    public function newsShowDetail()
    {
        $id      = $this->request->param('id');
        $detail  = $this->nav_news->where('id',$id)->select();
        $nav_id  = $this->nav_news->where('id',$id)->column('nav_id');
        if($detail){
            //查询上下新闻标题
            $top  = $this->getUpDownTitle($id,'nav_news',1);
            $down = $this->getUpDownTitle($id,'nav_news',0);
            $detail1 = getlistphoto($detail);
            //将内容格式化
            $detail1[0]['content'] = htmlspecialchars_decode($detail1[0]['content']);
            //阅读量+1
            $readnumber = $detail[0]['readnumber'] + 1;
            $list = $this->nav_news->save(['readnumber'=>$readnumber],['id'=>$id]);
            if($list){
                $list = [
                    'list' =>$detail1[0],
                    'top'  =>$top,
                    'down' =>$down,
                ];
                $this->apiSuccess($list);
            }else{
                $this->apiError('更新数据出错,请重试');
            }
        }else{
            $this->apiError('没有查到信息');
        }
    }

    /**
    *上一个下一个新闻
    *@return json
    **/
    public function newUpDown()
    {
        $id   = $this->request->param('id');
        $top  = $this->request->param('top'); 
        $info = $this->getUpDownId($id,"nav_news",$top);
            if (is_int($info)) {
                $detail  = $this->nav_news->where('id',$info)->select();
                if($detail){
                    //查询上下新闻标题
                    $top  = $this->getUpDownTitle($info,'nav_news',1);
                    $down = $this->getUpDownTitle($info,'nav_news',0);

                    $detail1 = getlistphoto($detail);
                    //将内容格式化
                    $detail1[0]['content'] = htmlspecialchars_decode($detail1[0]['content']);
                    //阅读量+1
                    $readnumber = $detail[0]['readnumber'] + 1;
                    $list = $this->nav_news->save(['readnumber'=>$readnumber],['id'=>$info]);
                    if($list){
                        $listInfo = [
                            'list'=>$detail1[0],
                            'top'=>$top,
                            'down'=>$down,
                        ];
                        $this->apiSuccess($listInfo);
                    }else{
                        $this->apiError('更新数据出错,请重试');
                    }
                }else{
                    $this->apiError('没有查到信息');
                }
            }else{
                $this->apiError('没有查到信息');
            }
       
    }



    /**
    *置顶的news显示
    *@return json
    **/
    public function topnewsshow()
    {
        $nav_id  = $this->request->param('nav_id');
        $this->request->param('limit') ? $limit = $this->request->param('limit') : $limit = 6;
        $newslist = $this->showTop($nav_id,$limit,'nav_news');
        if($newslist){
            $this->apiSuccess($newslist);
        }else{
            $this->apiError('没有查到信息');
        }
    }

    /**
    *获得上一篇下一篇的id
    *@return json
    **/
    public function getUpDownId($id,$model,$top)
    {
        $new = $this->$model->where('id',$id)->where('status',1)->select();
        //获得当前文章的nav_id
        $newsIdList = $this->$model->where('nav_id',$new[0]['nav_id'])->order('create_time desc,top desc')->column('id');
        if ($newsIdList) {
             //翻转数组,键和值翻转
            $list  = array_flip($newsIdList);
            //需要当前新闻的键
            $num   = $list[$id];
            $count = count($list)-1;
            if($top){
                if($num == 0){
                    return '';
                }else{
                    //上一篇新闻的id
                    $number = $num -1;
                    $nid = $newsIdList[$number];
                }
            }else{
                if($num == $count){
                    return '';
                }else{
                    //下一篇
                    $number = $num + 1;
                    $nid = $newsIdList[$number];
                }
            }
            return $nid;
        }
    }

    /**
    *获得上一篇下一篇的标题
    *@return json
    **/
    public  function  getUpDownTitle($id,$model,$top)
    {
        $info = $this->getUpDownId($id,$model,$top);
        if(is_int($info)){
            $detail  = $this->$model->where('id',$info)->select();
            return $detail[0]['title'];
        }else{
            return '';
        }
    }  


    /**
    *关于我们
    *@return json
    **/
    public function newsAboutUs()
    {
        $id      = $this->request->param('id');
        $detail  = $this->nav_news->where('nav_id',$id)->where('status',1)->select();
        if($detail){
            //查询上下新闻标题
            $detail1 = getlistphoto($detail);
            //将内容格式化
            $detail1[0]['content'] = htmlspecialchars_decode($detail1[0]['content']);
            //阅读量+1
            $readnumber = $detail[0]['readnumber'] + 1;
            $list = $this->nav_news->save(['readnumber'=>$readnumber],['id'=>$detail[0]['id']]);
            if($list){
                $this->apiSuccess($detail1);
            }else{
                $this->apiError('更新数据出错,请重试');
            }
        }else{
            $this->apiError('没有查到信息');
        }
    } 

    /**
    *置顶的显示
    *@return json
    **/
    private function showTop($nav_id,$limit,$model)
    {
        //判断传过来的是否还有子栏目
        $navlist = $this->nav_model->where('status',1)->select();
        //子栏目的导航id
        $arr = array2array($navlist,$nav_id);
        $map = [];
        if($arr){
            $map['nav_id'] = ['in',$arr];
            $list  = $this->$model->where($map)->where('status',1)->where('top',1)->order('create_time',' desc')->limit($limit)->select();
        }else{
            $list  = $this->$model->where('nav_id',$nav_id)->where('status',1)->where('top',1)->order('create_time',' desc')->limit($limit)->select();
        } 
        return $list;
    }

}